package com.configurator;

import javafx.scene.control.Button;

/**
 *
 * @author Sawior
 */
public class FieldData {

    private static final String SPACE_DEFAULT_STYLE = "space-creator-new";
    private static final String SPACE_FREE_STYLE = "space-creator-space";
    private static final String SPACE_DISABLED_STYLE = "space-creator-space-disable";
    private static final String SPACE_OCCUPIED_STYLE = "space-creator-space-occupied";
    private static final String SPACE_ROAD_STYLE = "space-creator-road";
    private final Integer x;
    private final Integer y;
    private String fieldType;
    private Integer spaceNumber;
    private String spaceType;
    private String spaceStatus;
    private final Integer level;

    public FieldData(String fieldType, Integer spaceNumber, String spaceType, String spaceStatus, Integer x, Integer y, Integer level) {
        this.fieldType = fieldType;
        this.spaceNumber = spaceNumber;
        this.spaceType = spaceType;
        this.spaceStatus = spaceStatus;
        this.x = x;
        this.y = y;
        this.level = level;
    }

    public boolean setToButton(Button space) {
        if (fieldType == null) {
            return false;
        }
        if (fieldType.equals("Stanowisko")) {
            if (spaceNumber == null || spaceType == null || spaceStatus == null) {
                return false;
            } else {
                space.setText(spaceNumber.toString() + " [" + convertSpaceTypeToChar() + "]");
                space.setId(spaceNumber.toString());
                if (spaceStatus.equals("Wolne") || spaceStatus.equals("Free")) {
                    if (space.getStyleClass().remove(SPACE_DEFAULT_STYLE)
                            || space.getStyleClass().remove(SPACE_DISABLED_STYLE)
                            || space.getStyleClass().remove(SPACE_OCCUPIED_STYLE)
                            || space.getStyleClass().remove(SPACE_ROAD_STYLE)) {
                        space.getStyleClass().add(SPACE_FREE_STYLE);
                    }
                } else {
                    if (spaceStatus.equals("Zajęte") || spaceStatus.equals("Occupied")) {
                        if (space.getStyleClass().remove(SPACE_DEFAULT_STYLE)
                                || space.getStyleClass().remove(SPACE_DISABLED_STYLE)
                                || space.getStyleClass().remove(SPACE_FREE_STYLE)
                                || space.getStyleClass().remove(SPACE_ROAD_STYLE)) {
                            space.getStyleClass().add(SPACE_OCCUPIED_STYLE);
                        }
                    } else {
                        if (space.getStyleClass().remove(SPACE_DEFAULT_STYLE)
                                || space.getStyleClass().remove(SPACE_FREE_STYLE)
                                || space.getStyleClass().remove(SPACE_OCCUPIED_STYLE)
                                || space.getStyleClass().remove(SPACE_ROAD_STYLE)) {
                            space.getStyleClass().add(SPACE_DISABLED_STYLE);
                        }
                    }
                }
                return true;
            }

        }
        if (fieldType.equals("Droga")) {
            if (space.getStyleClass().remove(SPACE_DEFAULT_STYLE)
                    || space.getStyleClass().remove(SPACE_DISABLED_STYLE)
                    || space.getStyleClass().remove(SPACE_OCCUPIED_STYLE)
                    || space.getStyleClass().remove(SPACE_FREE_STYLE)) {

                space.getStyleClass().add(SPACE_ROAD_STYLE);
            }
            space.setId(null);
            space.setText("");

            return false;
        }
        if (fieldType.equals("Puste")) {
            if (space.getStyleClass().remove(SPACE_ROAD_STYLE)
                    || space.getStyleClass().remove(SPACE_DISABLED_STYLE)
                    || space.getStyleClass().remove(SPACE_OCCUPIED_STYLE)
                    || space.getStyleClass().remove(SPACE_FREE_STYLE)) {

                space.getStyleClass().add(SPACE_DEFAULT_STYLE);
            }
            space.setId(null);
            space.setText("");

            return false;
        }
        return false;
    }

    private String convertSpaceTypeToChar() {
        if (spaceType.equals("Osobowe") || spaceType.equals("Personal")) {
            return "O";
        }
        if (spaceType.equals("Dostawcze") || spaceType.equals("Van")) {
            return "D";
        }
        if (spaceType.equals("Motor") || spaceType.equals("Motorcycle")) {
            return "M";
        }
        if (spaceType.equals("Przyczepka") || spaceType.equals("Trailer")) {
            return "P";
        }
        if (spaceType.equals("Specialne") || spaceType.equals("Special")) {
            return "S";
        }

        return "O";
    }

    public String getSpaceTypeToXML() {
        return convertSpaceTypeToDataBaseEnum();
    }

    public String getSpaceStatusToXML() {
        return convertSpaceStatusToDataBaseEnum();
    }

    private String convertSpaceStatusToDataBaseEnum() {
        if (spaceStatus.equals("Wolne") || spaceStatus.equals("Free")) {
            return "Free";
        }
        if (spaceStatus.equals("Zajęte") || spaceStatus.equals("Occupied")) {
            return "Occupied";
        }
        if (spaceStatus.equals("Niedostępne") || spaceStatus.equals("Disabled")) {
            return "Disabled";
        }

        return "Free";
    }

    private String convertSpaceTypeToDataBaseEnum() {
        if (spaceType.equals("Osobowe") || spaceType.equals("Personal")) {
            return "Personal";
        }
        if (spaceType.equals("Dostawcze") || spaceType.equals("Van")) {
            return "Van";
        }
        if (spaceType.equals("Motor") || spaceType.equals("Motorcycle")) {
            return "Motorcycle";
        }
        if (spaceType.equals("Przyczepka") || spaceType.equals("Osobowe")) {
            return "Trailer";
        }
        if (spaceType.equals("Specialne") || spaceType.equals("Special")) {
            return "Special";
        }
        if (spaceType.equals("Wszystkie") || spaceType.equals("All")) {
            return "All";
        }
        return "Personal";
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public Integer getSpaceNumber() {
        return spaceNumber;
    }

    public void setSpaceNumber(Integer spaceNumber) {
        this.spaceNumber = spaceNumber;
    }

    public String getSpaceType() {
        return spaceType;
    }

    public void setSpaceType(String spaceType) {
        this.spaceType = spaceType;
    }

    public String getSpaceStatus() {
        return spaceStatus;
    }

    public void setSpaceStatus(String spaceStatus) {
        this.spaceStatus = spaceStatus;
    }

    public Integer getX() {
        return x;
    }

    public Integer getY() {
        return y;
    }

    public Integer getLevel() {
        return level;
    }

    @Override
    public String toString() {
        return "FieldData{" + "Kolumna=" + x + ", Wiersz=" + y + ", fieldType=" + fieldType + ", spaceNumber=" + spaceNumber + ", spaceType=" + spaceType + ", spaceStatus=" + spaceStatus + ", level=" + level + '}';
    }
}
